/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.api.actioninterface.method;

import com.inspur.edp.bef.bizentity.operation.BizOperation;
import com.inspur.edp.bef.component.base.GspComponent;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrMethod;
import com.inspur.edp.bef.component.detailcmpentity.vm.VMMethod;
import com.inspur.edp.bff.designtime.generator.api.VmApiGenContext;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.formserver.viewmodel.action.MappedCdpAction;
import com.inspur.edp.formserver.viewmodel.action.ViewModelAction;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import java.util.ArrayList;

public class MgrActionBuildUtil {

  public static void addActionGenertors(VmApiGenContext apiGenCtx,
      ArrayList<BaseGenerator> childGenerators) {

    for (ViewModelAction vmAction : apiGenCtx.getVmGenContext().getVm().getActions()) {
      switch (vmAction.getType()) {
        case BEAction:
          BizOperation beMgrAction = apiGenCtx.getVmGenContext()
              .getRefBizEntity().getBizMgrActions().getItem(vmAction.getMapping().getTargetObjId());
          if (beMgrAction == null) {
            throw new RuntimeException(
                "vm[" + apiGenCtx.getVmGenContext().getVm() + "]的动作[" + vmAction.getCode()
                    + "]对应的be动作没有找到，be动作Id为" + vmAction.getMapping().getTargetObjId());
          }
          BEMgrMethod method = getBeMgrMethod(apiGenCtx, beMgrAction.getComponentId(),
              beMgrAction.getComponentPkgName());
          ActionInterfaceMethodGenerator gen = new ActionInterfaceMethodGenerator(method,
              vmAction.getCode(), method.getParams(), apiGenCtx);
          childGenerators.add(gen);
          break;

        case VMAction:
          MappedCdpAction action = (MappedCdpAction) vmAction;
          VMMethod vmMethod = null;
          try {
            vmMethod = getVmMgrMethod(apiGenCtx, action.getComponentEntityId(),
                action.getComponentPkgName());
          } catch (Exception e) {
            throw new RuntimeException("方法" + action.getCode() + "对应的构件元数据获取异常", e);
          }

          ActionInterfaceMethodGenerator vmGen = new ActionInterfaceMethodGenerator(vmMethod,
              vmAction.getCode(), vmMethod.getParams(), apiGenCtx);
          childGenerators.add(vmGen);
          break;
      }
    }
  }

  private static BEMgrMethod getBeMgrMethod(VmApiGenContext apiGenCtx, String compId,
      String compPkgName) {
    GspMetadata metadata = apiGenCtx.getVmGenContext().getMetadataPool()
        .getMetadata(compPkgName, compId);
    GspComponent comp = (GspComponent) metadata.getContent();
    if (!comp.getComponentType().equals("BEMgrComponent")) {
      throw new RuntimeException("错误的类型");
    }
    return (BEMgrMethod) comp.getMethod();
  }

  private static VMMethod getVmMgrMethod(VmApiGenContext apiGenCtx, String compId,
      String compPkgName) {
    GspMetadata metadata = apiGenCtx.getVmGenContext().getMetadataPool()
        .getMetadata(compPkgName, compId);
    if (metadata == null) {
      throw new RuntimeException("构件元数据没有获取到，构件id为" + compId);
    }
    GspComponent comp = (GspComponent) metadata.getContent();
    if (!comp.getComponentType().equals("VMComponent")) {
      throw new RuntimeException("错误的类型：" + comp.getComponentType());
    }
    return (VMMethod) comp.getMethod();
  }
}
